﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using OpenCvSharp;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace MultiRobot
{
    class CamServer
    {
        private IplImage FindFace;
        private bool isDone = false;
        public void DoWork()
        {
            Program.form1.printlog("Cam Thread start...");
            using (CvCapture cap = CvCapture.FromCamera(0))
            {
                cap.FrameWidth = Program.form1.pictureBoxIpl1.Width;
                cap.FrameHeight = Program.form1.pictureBoxIpl1.Height;
                while (!isDone)
                {
                    IplImage image = cap.QueryFrame();
                    

                    //Program.form1.pictureBoxIpl1.Image = FaceDetect(image).ToBitmap();
                    addgrid(image);
                    
                    Program.form1.pictureBoxIpl1.Image = image.ToBitmap();
                    Program.form1.picturerefresh();
                    
                }
                

            }
        }
        public void addgrid(IplImage image) {

            int width = image.Width;
            int height = image.Height;
            int cellsize = 10;
            CvColor black = new CvColor(0, 0, 0);
            for (int i = 0; i < width / cellsize; i++) {
                image.Line(new CvPoint(0, i*cellsize), new CvPoint(width, i*cellsize), black);
                image.Line(new CvPoint(i * cellsize, 0), new CvPoint(i * cellsize, height), black);
            }
            


        }
        public void fillrect() { 
            
        }


        public IplImage FaceDetect(IplImage src)
        {
            // CvHaarClassifierCascade, cvHaarDetectObjects
            // 얼굴을 검출하기 위해서 Haar 분류기의 캐스케이드를 이용한다
            CvColor[] colors = new CvColor[]{
                new CvColor(0,0,255),
                new CvColor(0,128,255),
                new CvColor(0,255,255),
                new CvColor(0,255,0),
                new CvColor(255,128,0),
                new CvColor(255,255,0),
                new CvColor(255,0,0),
                new CvColor(255,0,255),
            };
            const double scale = 1.04;
            const double scaleFactor = 1.139;
            const int minNeighbors = 2;
            using (IplImage img = src.Clone())
            using (IplImage smallImg = new IplImage(new CvSize(Cv.Round(img.Width / scale), Cv.Round(img.Height / scale)), BitDepth.U8, 1))
            {
                // 얼굴 검출용의 화상의 생성
                using (IplImage gray = new IplImage(img.Size, BitDepth.U8, 1))
                {
                    Cv.CvtColor(img, gray, ColorConversion.BgrToGray);
                    Cv.Resize(gray, smallImg, Interpolation.Linear);
                    Cv.EqualizeHist(smallImg, smallImg);
                }
                using (CvHaarClassifierCascade cascade = CvHaarClassifierCascade.FromFile(Application.StartupPath + "\\" + "haarcascade_frontalface_alt2.xml"))
                using (CvMemStorage storage = new CvMemStorage())
                {
                    storage.Clear();
                    // 얼굴의 검출
                    CvSeq<CvAvgComp> faces = Cv.HaarDetectObjects(smallImg, cascade, storage, scaleFactor, minNeighbors, 0, new CvSize(80, 80));
                    // 검출한 얼굴에 원을 그린다
                    for (int i = 0; i < faces.Total; i++)
                    {
                        CvRect r = faces[i].Value.Rect;
                        CvPoint center = new CvPoint
                        {
                            X = Cv.Round((r.X + r.Width * 0.5) * scale),
                            Y = Cv.Round((r.Y + r.Height * 0.5) * scale)
                        };
                        int radius = Cv.Round((r.Width + r.Height) * 0.25 * scale);
                        img.Circle(center, radius, colors[i % 8], 3, LineType.AntiAlias, 0);
                    }
                }
                FindFace = img.Clone();
                return FindFace;
            }
        }
        public void setDone()
        {
            isDone = true;
        }
    }
}
